using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using MinimalistFoodStreet.BackEnd.Infrastructure.Attributes;
using MinimalistFoodStreet.BackEnd.Infrastructure.Dtos.Request.Tables;
using MinimalistFoodStreet.BackEnd.Infrastructure.Dtos.Response;
using MinimalistFoodStreet.BackEnd.Infrastructure.Dtos.Response.Tables;
using MinimalistFoodStreet.BackEnd.Infrastructure.EFCore;
using MinimalistFoodStreet.BackEnd.Server.TableEFCore;
using MinimalistFoodStreet.BackEnd.Server.Server.RequestExtend.Tables;
using MinimalistFoodStreet.BackEnd.Server.Server.ResponseExtend.Tables;
using MinimalistFoodStreet.BackEnd.Server.IServer.Tables;

namespace MinimalistFoodStreet.BackEnd.Server.Server.Tables
{
    [InjectClass]
    public class VarifysServer : IVarifysServer
    {
        private readonly ServerRepository _serverRepository;
        public VarifysServer(DbContext dbContext)
        {
            _serverRepository = new ServerRepository(dbContext);
        }

        public async Task<ResponseApiDto> AddAsync(RequestCreateVarifysDto dto)
        {
            int affectedRows = await _serverRepository.varifysEFCore.AddAsync(dto.ToCreateEntity());
            return ResponseApiDto.Create(affectedRows);
        }

        public async Task<ResponseApiDto> UpdateAsync(RequestUpdateVarifysDto dto)
        {
            Varify varify = await _serverRepository.varifysEFCore.Query(d => d.VId == dto.id).SingleOrDefaultAsync();
            if (varify == null)
            {
                return ResponseApiDto.Fail("该实例不存在");
            }
            varify.VIsUsed = dto.isUsed;
            varify.VUseTime = dto.useTime;
            int affectedRows = await _serverRepository.varifysEFCore.UpdateAsync(varify);
            return ResponseApiDto.Create(affectedRows);
        }

        public async Task<ResponseApiDto> DeleteAsync(RequestDeleteVarifysDto dto)
        {
            int affectedRows = await _serverRepository.varifysEFCore.Query(d => d.VId == dto.id).ExecuteDeleteAsync();
            if (affectedRows == 0)
            {
                return ResponseApiDto.Fail("该实例不存在");
            }
            return ResponseApiDto.Create(affectedRows);
        }

        public async Task<ResponseApiDto> DetailAsync(int id)
        {
            Varify varify = await _serverRepository.varifysEFCore.Query(d => d.VId == id).SingleOrDefaultAsync();
            if (varify == null)
            {
                return ResponseApiDto.Fail("该实例不存在");
            }
            return ResponseApiDto.Ok("数据已获取", varify.ToResponseDto());
        }

        public async Task<ResponseApiDto> PaginationAsync(int page, int limit, string? key)
        {
            int total = 0;
            List<Varify> varifys = await _serverRepository.varifysEFCore.Pagination(d => true, false, d => d.VId, out total, page, limit).ToListAsync();
            List<ResponseVarifysDto> responseVarifysDtos = varifys.Select(d => d.ToResponseDto()).ToList();
            return ResponseApiDto.Ok("数据已获取", new { data = responseVarifysDtos, total = total });
        }
    }
}